﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Client;
using System.Text.RegularExpressions;
using Dropit.BlogPageProvider.Blog;

namespace Dropit.BlogPageProvider.Blogger
{
	public class Provider : IBlogProvider
	{
		private static Dictionary<int, Service> _services = new Dictionary<int, Service>();

		public List<IBlog> GetBlogs(Connection connection)
		{
			return Blog.List(GetService(connection)).Select<Blog, IBlog>(post => post as IBlog).ToList();
		}

		private Service GetService(Connection connection)
		{
			if (!_services.ContainsKey(connection.Id))
			{
				_services.Add(connection.Id, Utility.CreateService(connection));
			}
			return _services[connection.Id];
		}

		public IBlog GetBlogById(Connection connection, string id)
		{
			BloggerId bloggerId;
			if (BloggerId.TryParse(id, out bloggerId))
			{
				return (IBlog)Blog.Load(GetService(connection), bloggerId);
			}
			return null;
		}

		public List<IPost> GetPosts(Connection connection, string id)
		{
			BloggerId bloggerId;
			if (BloggerId.TryParse(id, out bloggerId))
			{
				return Post.List(GetService(connection), bloggerId).Select<Post, IPost>(post => post as IPost).ToList();
			}
			return new List<IPost>(0);
		}

		public IPost GetPostById(Connection connection, string id)
		{
			BloggerId bloggerId;
			if (BloggerId.TryParse(id, out bloggerId))
			{
				return (IPost)Post.Load(GetService(connection), bloggerId);
			}
			return null;
		}

		public List<IComment> GetComments(Connection connection, string id)
		{
			BloggerId bloggerId;
			if (BloggerId.TryParse(id, out bloggerId))
			{
				return Comment.List(GetService(connection), bloggerId).Select<Comment, IComment>(comment => comment as IComment).ToList(); ;
			}
			return new List<IComment>(0);
		}

		public IComment GetCommentById(Connection connection, string id)
		{
			BloggerId bloggerId;
			if (BloggerId.TryParse(id, out bloggerId))
			{
				return (IComment)Comment.Load(GetService(connection), bloggerId);
			}
			return null;
		}

		public Post CreatePost(BloggerId id, Post post)
		{
			return null;
		}

		public static void DeletePost(BloggerId id)
		{
			/*
			Blog blog = GetBlog(id);
			if (blog != null)
			{
				AtomEntry entry = GetFeedEntries(blog.PostFeedUrl).First<AtomEntry>(e => e.Id.Uri.ToString() == id.PostId);
				if (entry != null)
				{
					entry.Delete();
				}
			}
			 */
		}

		public static void SavePost(Post post)
		{/*
			AtomEntry entry = InternalPosts.First<AtomEntry>(ae => ae.Id.AbsoluteUri == post.Id);
			if (entry != null)
			{
				entry.Title.Text = post.Heading;
				entry.Content.Content = post.Content;
				entry.Update();
			}*/
		}

		public static void DeleteComment(BloggerId id)
		{
		}

		public IPost CreatePost(Connection connection)
		{
			return new Post();
		}

		public IComment CreateComment(Connection connection)
		{
			return new Comment();
		}

		public string SavePost(Connection connection, string parentId, IPost post)
		{
			BloggerId bloggerId;
			if (post != null && BloggerId.TryParse(parentId, out bloggerId))
			{
				return Post.SaveNew(Utility.CreateService(connection), bloggerId, post).BloggerId.ToString();
			}
			return string.Empty;
		}

		public string SaveComment(Connection connection, string parentId, IComment comment)
		{
			BloggerId bloggerId;
			if (comment != null && BloggerId.TryParse(parentId, out bloggerId))
			{
				return Comment.SaveNew(Utility.CreateService(connection), bloggerId, comment).BloggerId.ToString();
			}
			return string.Empty;
		}

		public void DeletePost(Connection connection, string id)
		{
			BloggerId bloggerId;
			if (BloggerId.TryParse(id, out bloggerId))
			{
				Post.Delete(Utility.CreateService(connection), bloggerId);
			}			
		}

		public void DeleteComment(Connection connection, string id)
		{
			BloggerId bloggerId;
			if (BloggerId.TryParse(id, out bloggerId))
			{
				Comment.Delete(Utility.CreateService(connection), bloggerId);
			}
		}

		public void UpdatePost(Connection connection, IPost post)
		{
			BloggerId bloggerId;
			if (post != null && BloggerId.TryParse(post.Id, out bloggerId))
			{
				Post.Update(Utility.CreateService(connection), bloggerId, post);
			}
		}

		public void UpdateComment(Connection connection, IComment comment)
		{
			BloggerId bloggerId;
			if (comment != null && BloggerId.TryParse(comment.Id, out bloggerId))
			{
				Comment.Update(Utility.CreateService(connection), bloggerId, comment);
			}
		}
	}
}